<?php

namespace App\Http\Controllers\Admin\Content;

use App\Http\Controllers\Controller;
use App\Repositories\Article\ArticleCategoryRepository;
use App\Repositories\Article\ArticleRepository;
use App\Repositories\Article\ArticleTagRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class ArticleController extends Controller
{
    protected array $validateMessages = [
        'title.*' => '请输入标题',
        'category_id.*' => '请选择所属分类',
        'cover.*' => '请上传图片',
        'keywords.*' => '请输入关键词',
        'description.*' => '请输入描述',
        'author.*' => '请输入作者',
        'content.*' => '请输入内容',
        'status.*' => '请输入正确的状态',
        'sort.*' => '请输入排序',
    ];

    public function __construct(Request $request, ArticleRepository $repository)
    {
        $this->request = $request;
        $this->repository = $repository;
    }

    public function index(ArticleCategoryRepository $categoryRepository,ArticleTagRepository $tagRepository)
    {
        $params = $this->request->all();
        $list = $this->repository->paginate($params);
        return $this->inertia('admin/content/article/index', [
            'data' => $list->items(),
            'pagination' => [
                'total' => $list->total(),
                'page' => $list->currentPage(),
                'pageSize' => $list->perPage(),
                'showTotal' => true,
            ],
            'tags' => $tagRepository->getSelect(),
            'categories' => $categoryRepository->getSelect(),
            'filters' => $params,
            'title' => '文章管理',
        ]);
    }

    public function show($id): array
    {
        $row = $this->repository->getById($id);
        $row->tag_ids = $row->tags()->pluck('id')->toArray() ?? [];

        return [
            'data' => $row,
        ];
    }

    public function store()
    {
        $params = $this->validate(
            $this->request,
            [
                'title' => 'required|string',
                'category_id' => 'required|integer',
                'cover' => 'nullable|string',
                'keywords' => 'nullable|string',
                'description' => 'nullable|string',
                'author' => 'nullable|string',
                'markdown' => 'nullable|string',
                'status' => 'required|integer',
                'sort' => 'nullable|integer',
                'views' => 'nullable|integer',
            ],
            $this->validateMessages
        );
        $params['content'] = Str::markdown($params['markdown'], ['html_input' => 'strip','allow_unsafe_links' => false]);
        $row = $this->repository->create($params);
        $tag_ids = $this->request->input('tag_ids', []);
        $row->tags()->sync($tag_ids);
        return $this->responseCreated();
    }

    public function update($id)
    {
        $params = $this->validate(
            $this->request,
            [
                'title' => 'required|string',
                'category_id' => 'required|integer',
                'cover' => 'nullable|string',
                'keywords' => 'nullable|string',
                'description' => 'nullable|string',
                'author' => 'nullable|string',
                'markdown' => 'nullable|string',
                'status' => 'required|integer',
                'sort' => 'nullable|integer',
                'views' => 'nullable|integer',
            ],
            $this->validateMessages
        );
        $tag_ids = $this->request->input('tag_ids', []);
        $row = $this->repository->getById($id);
        $params['content'] = Str::markdown($params['markdown'], ['html_input' => 'strip','allow_unsafe_links' => false]);
        $row->update($params);
        $row->tags()->sync($tag_ids);
        return $this->responseSuccess();
    }

    public function destroy($id)
    {
        $row = $this->repository->getById($id);
        $row->tags()->detach();
        $row->delete();
        return $this->responseDeleted();
    }
}
